Test Failed
Push — develop ( 836cd9...6ec826 )
by Endre
15:03
created

Router.updatePage   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import {IObserver} from '../Observer/Observer';
2
3
export interface IPageData {
4
  name: string,
5
  url: string,
6
  depth: number
7
}
8
9
export default class Router {
10
  currentPage: IObserver<IPageData>;
11
  history: History;
12
13
  constructor(pageObserver: IObserver<IPageData>, history: History) {
14
    this.currentPage = pageObserver;
15
    this.history = history;
16
  }
17
18
  attachTo(window: Window) {
19
    window.addEventListener('popstate', this.onHistoryChange.bind(this));
20
  }
21
22
  changePage(newPage: IPageData): void {
23
    const currentPage = this.currentPage.value;
24
    if (currentPage.name == newPage.name) {
25
      return;
26
    }
27
28
    this.history.pushState(newPage, newPage.name, newPage.url);
29
30
    this.updatePage(newPage);
31
  }
32
33
  updatePage(page: IPageData): void {
34
    this.currentPage.value = page;
35
  }
36
37
  onHistoryChange(event: PopStateEvent) {
38
    const newPage: IPageData = event.state as IPageData;
39
    this.updatePage(newPage);
40
  }
41
}